import Request from "luch-request"
import {
	config
} from "@/config.js";
import methods from "@/utils/methods.js";
// let baseUrl = process.env.NODE_ENV === 'development' ? config.dev_url : config.base_url;
let baseUrl = config.base_url;
let scrollstr = false;
const http = new Request({
	baseURL: baseUrl,
	timeout: 30000,
})
http.interceptors.request.use((req) => {
	let sysInfo = {}
	scrollstr = req.params.scrollstr || false
	uni.getSystemInfo({
		success(res) {
			sysInfo = res
		}
	})
	req.header = {
		Authorization: 'Bearer ' + uni.getStorageSync("login"),
		// PlatformInfo: encodeURI(JSON.stringify(sysInfo))
	}
	return req
})
http.interceptors.response.use((response) => { 
	let statusCode = response.statusCode
	let code = response.data.code
	let msg = response.data.msg || '请求错误'
	methods.loadToShow();
	try {
		methods.hideLoading();
		if (scrollstr) {
			return response && response.data
		}
		if (statusCode == 200 && code == 200) {
			return response && response.data
		}
		if (statusCode == 200 && code == 500) {
			methods.showToast(msg);
			//if (!methods.readData('useId')) return methods.navLaunch('/pages/login/login');
			return response && response.data
		}
		if (statusCode == 200 && code == 400) {
			methods.showToast(msg);
			return response && response.data
		}
		if (statusCode == 200 && code == 404) {
			methods.showToast(msg);
			return response && response.data
		}
		if (statusCode == 200 && code == 401) {
			methods.clearData();
			return methods.navLaunch('/pages/login/login');
		}

	} catch (error) {
		return methods.showToast(error || '请求错误');
	}
	return response.data;
})
export default http